//
//  MineViewController.m
//  share_business
//
//  Created by baidufz on 2017/8/26.
//  Copyright © 2017年 wyl. All rights reserved.
//

#import "MineViewController.h"
#import "PersonalInfoCardCell.h"
#import "Text2Cell.h"
#import "MenuCell.h"
#import "SpaceCell.h"
#import "MenuBarCell.h"
#import "UIView+Extension.h"
#import "UINavigationBar+BackgroundColor.h"
#import "ManageAddressViewController.h"
#import "BaseNavigationController.h"
#import "SettingViewController.h"
#import "MyCardViewController.h"
#import "IntegralDetailViewController.h"
#import "UICollectionView+RegisterCell.h"
#import "CooperationChannelViewController.h"
#import "CouponViewController.h"
#import "ManageBankCardViewController.h"
#import "MyTaskViewController.h"


#define ViewWidth        ([[UIScreen mainScreen] bounds].size.width - 10 * 2)
#define MenuWidth   ViewWidth / 4 -10

@interface MineViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, PersonalInfoCardDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (nonatomic, retain) NSArray *myOrderMenu;
@property (nonatomic, retain) NSArray *myTaskMenu;
@property (nonatomic, retain) NSArray *essentialToolMenu;

@property (nonatomic, retain) UIImageView *navigationImageView;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self registerCells];
    [self initMenuData];
    [self initView];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [self topLeftItemButton:ImageNamed(@"mine_setting") title:@"设置"];
    [self topRightItemButton:ImageNamed(@"mine_no_news") image2:nil title1:@"消息" title2:nil];
    
    self.navigationImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    self.navigationImageView.hidden = YES;
    
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor colorWithHexString:@"#FF9800"]];
}

-(void)leftAction:(UIButton *)button{
    [self presentViewController:[[UINavigationController alloc]initWithRootViewController:[SettingViewController new]] animated:YES completion:nil];
}


- (void)viewWillDisappear:(BOOL)animated {
    self.navigationImageView.hidden = NO;
    
    [self.navigationController.navigationBar lt_reset];

}

//实现的方法
-(UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

- (void)initView {
    [self setTopBarTitle:@"我的"];
    
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.showsVerticalScrollIndicator = NO;

}

- (void)registerCells {
    [_collectionView registerHeaderCell:[PersonalInfoCardCell class]];
    [_collectionView registerHeaderCell:[Text2Cell class]];
    [_collectionView registerCell:[MenuCell class]];
    [_collectionView registerCell:[MenuBarCell class]];
    [_collectionView registerFooterCell:[SpaceCell class]];

}

- (void)initMenuData {
    _myOrderMenu = @[@[@"待付款",@"mine_payment", ^(){}],
  @[@"待发货",@"mine_shipping", ^(){}],
  @[@"待收货",@"mine_receive_goods", ^(){}],
  @[@"待评价",@"mine_appraise", ^(){}]];
    
    _myTaskMenu = @[@[@"任务",@"mine_mission", ^(){}],
  @[@"收益",@"mine_earnings", ^(){}],
  @[@"完成",@"mine_finish", ^(){}]];
    
    _essentialToolMenu = @[@[@"我的名片",@"mine_business_card", ^(){
                                MyCardViewController *ctrl = [MyCardViewController new];
                                [self showViewController:ctrl];
                            }],
                           @[@"收货地址",@"mine_address", ^(){
                               ManageAddressViewController *ctrl = [ManageAddressViewController new];
                               [self showViewController:ctrl];
                           }],
  @[@"银行卡管理",@"mine_bank_card", ^(){
      [self showViewController:[ManageBankCardViewController new]];
  }],
  @[@"批量进货",@"mine_replenish_stock", ^(){}],
  @[@"客服",@"mine_customer_service", ^(){}],
  @[@"优惠券",@"mine_coupon", ^(){
      [self showViewController:[CouponViewController new]];
  }],
  @[@"售后／退货",@"mine_after_sale", ^(){}],
  @[@"申请合作",@"mine_cooperation", ^(){
      CooperationChannelViewController *ctrl = [CooperationChannelViewController new];
      [self showViewController:ctrl];
  }]];

}

- (void)showIntegralDetail:(IntegralDetailType)type {
    [self showViewController:[IntegralDetailViewController new]];
}

- (void)showViewController:(UIViewController *)vc {
//    [self presentViewController:[[UINavigationController alloc]initWithRootViewController:vc] animated:YES completion:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 6;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    int number = 0;
    switch (section) {
        case 0:
        case 3:
        case 4:
            number = 0;
            break;
        case 1:
            number = 1;
            break;
        case 2:
            number = 1;
            break;
        case 5:
            number = 2;
            break;
        default:
            break;
    }
    return number;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader] && indexPath.section == 0) {
         PersonalInfoCardCell *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([PersonalInfoCardCell class]) forIndexPath:indexPath];
        header.delegate = self;
        [header setBorder:1 cornerRadius:0];
        return header;
    } else if ([kind isEqualToString:UICollectionElementKindSectionHeader] && indexPath.section >= 1 && indexPath.section <=5) {
             Text2Cell *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([Text2Cell class]) forIndexPath:indexPath];
        switch (indexPath.section) {
            case 1:
                [header setTitle:@"我的订单" right:@"查看全部"];
                [header setUIGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(queryOrder)]];
                break;
            case 2:
                [header setTitle:@"我的任务" right:@"查看全部"];
                [header setUIGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(queryTask)]];
                break;
            case 3:
                [header setTitle:@"我的朋友" right:@"目前共有好友：99人"];
                break;
            case 4:
                [header setTitle:@"我的成果" right:@"共完成商品推广：100份"];
                break;
            case 5:
                [header setTitle:@"必备工具" right:@""];
                header.arrowIcon.hidden = YES;
                break;
            default:
                break;
        }
        [header setBorder:1 cornerRadius:0];

        return header;

    } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        SpaceCell *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([SpaceCell class]) forIndexPath:indexPath];
        
        return footer;
    }
    return nil;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MenuBarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MenuBarCell class]) forIndexPath:indexPath];
    [cell setBorder:1 cornerRadius:0];
    if (indexPath.section == 1) {
        [cell setup:_myOrderMenu];
    } else if (indexPath.section == 2) {
        [cell setup:_myTaskMenu];
    } else if (indexPath.section == 5) {
        NSRange range;
        if (indexPath.row == 0) {
            range = NSMakeRange(0, 4);
        } else {
            range = NSMakeRange(4, 4);
        }
        [cell setup:[_essentialToolMenu subarrayWithRange:range]];
            }
    return  cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(ViewWidth, MenuWidth);
}

/** 底部的尺寸*/
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(1, 30);
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    CGSize size = CGSizeMake(0, 0);
    if (section == 0) {
        size = CGSizeMake(ViewWidth, 160);
    } else if (section > 0 && section <= 5) {
        size = CGSizeMake(ViewWidth, 40);
    }
    return size;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"section -- %ld, row -- %ld",(long)indexPath.section,(long)indexPath.row);
}

- (void)queryOrder {
    
}

- (void)queryTask {
    [self showViewController:[MyTaskViewController new]];
}


@end
